/*
 * Project it.battlehorse.rcp.tools.actors.sample
 * Created on Nov 19, 2005
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */ 
package it.battlehorse.rcp.tools.actors.sample;

import it.battlehorse.rcp.sl.annot.Injected;
import it.battlehorse.rcp.sl.annot.Serviceable;
import it.battlehorse.rcp.tools.actors.IActorRunner;

import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.part.ViewPart;

/**
 * 
 * @author battlehorse
 * @since Nov 26, 2005
 */
@Serviceable
public class SampleActorView extends ViewPart {
	
	public static final String ID_VIEW = "it.battlehorse.rcp.tools.actors.sample.SampleActorView";
	
	private FormToolkit toolkit;
	
	private ScrolledForm form;
	
	private Text t;
	
	private String numSecs;
	
	private IActorRunner runner;
	
	@Injected
	public void setActorRunner(IActorRunner runner) {
		this.runner = runner;
	}
	
	@Override public void init(IViewSite site, IMemento memento) throws PartInitException {
		super.init(site,memento);
		
		if (memento != null)
			numSecs = memento.getString("NUM_SECONDS");
		else
			numSecs = "";
	}
	
	
	@Override public void createPartControl(Composite parent) {
		toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createScrolledForm(parent);
		
		form.setText("Sample Actor");
		
		GridLayout layout = new GridLayout(2,false);
		form.getBody().setLayout(layout);

		toolkit.createLabel(form.getBody(),"Number of second(s):");		
		
		t = toolkit.createText(form.getBody(),numSecs);
		t.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Button b = toolkit.createButton(form.getBody(), "Click to run the sample job", SWT.PUSH | SWT.FLAT);
		GridData gd = new GridData();
		gd.horizontalSpan = 2;
		b.setLayoutData(gd);		
		b.addSelectionListener(runner.newButtonActorLauncher(SampleActor.ID_ACTOR));
		
		toolkit.paintBordersFor(form.getBody());
	}
	
	@Override public void setFocus() {
		form.setFocus();		
	}
	
	@Override public void dispose() {		
		toolkit.dispose();
		super.dispose();		
	}
	
	@Override public void saveState(IMemento memento) {
		memento.putString("NUM_SECONDS",t.getText());
	}
	
	public String getSeconds() {
		return t.getText();
	}
	
}
